package zj.com.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import zj.com.utils.LocalDownJpg;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Random;

/**
 * 淘宝商家图片爬取，每次需要重新填写cookies
 */
public class TaobaoCrawler {

    public static void main(String[] args) {
        try {
            String p="%B5%D8%BF%B1%BB%FA";
            String url = "https://s.taobao.com/search?q="+p+"&s_from=newHeader&ssid=s5-e&search_type=item&sourceId=tb.item&bcoffset=4&p4ppushleft=2%2C48&sort=price-desc&ntoffset=4";
            URL realUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("Referer", "https://s.taobao.com/search?q="+p+"&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306");
            connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0");
            String cookie="JSESSIONID=2DEF1661E77AF671B702FD734C9E5B9D;_m_h5_tk=fe417b0fe993d1c39bef375fea2ccf3b_1627384325883;_m_h5_tk_enc=b8d4c8cc181e3abfafdb972e2e6158b5;_samesite_flag_=true;_tb_token_=efe91a1745376;_uab_collina=162737605051562493616653;_umdata=G407C2BF3B8F1D25DFEEC356CADA9967E4A5D6D;alitrackid=www.taobao.com;atpsida=daa5a7b4b406dd33f5ae8e24_1627376167_26;atpsidas=aef56e3cdddabd5781f2418e_1627376167_27;aui=1988920402;cad=17ae72be0d6-5254498423284286610001;cap=8cb5;cdpid=Uojcg6UvzNNLtA%253D%253D; cna=dUqFGVOocRoCAXQXou7/AarO;cna=dUqFGVOocRoCAXQXou7/AarO;cnaui=1988920402;cookie2=18c1bf91cac518cfdb62ad6c84645d3d;enc=gylSEqjmnKZk1vpdoTY0vEoak74SBy49XopXrPGkTBjsV1Xo%2Bvgjg9Dj7hdteMLi8SyYSkYvjwdtBxLdn9abow%3D%3D;hng=CN%7Czh-CN%7CCNY%7C156;isg=BLCw7hi3pEys6nhIkwtpRXDdgX4C-ZRDpakQKKoBfIveZVAPUglk0wZXvW0FdUwb;l=eBP9wBicjQDU-SvoBOfwourza77OSIRAguPzaNbMiOCPOYCp5kcCW6TnPG89C3GVh6lvR3rEQAfYBeYBqIv4n5U62j-la_kmn;lastalitrackid=www.taobao.com;mt=ci=0_0;sca=8de11c9d;t=27c3a2361d7a320f12b5c150baa71d92;tbsa=a4b9e981a949136d88aa3a7b_1627376167_25;tfstk=cVahBVt4HkoQn-ogcwgQtKiUgLsOZNTE3zz_7zOt-3FuvkaNistw0G4GsvA2L31..;thw=cn;tracknick='';xlly_s=1;";
            connection.setRequestProperty("Cookie",cookie);
            // 建立实际的连接
            connection.connect();
            //请求成功
            System.out.println("请求状态："+connection.getResponseCode());
//	        if (connection.getResponseCode() == 200) {
            InputStream is = connection.getInputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            //10MB的缓存
            byte[] buffer = new byte[10485760];
            int len = 0;
            while ((len = is.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            String jsonString = baos.toString();
//            System.out.println("jsonString:"+jsonString);
            String trim = jsonString.substring(jsonString.indexOf("g_page_config"), jsonString.indexOf("g_srp_loadCss")).trim();
            String replace = trim.replace("g_page_config = ", "");
            String substring = replace.substring(0, replace.lastIndexOf(";"));
            System.out.println(substring);
            JSONObject jsonObject = JSON.parseObject(substring);
            JSONArray jsonArray = jsonObject.getJSONObject("mods").getJSONObject("itemlist").getJSONObject("data").getJSONArray("auctions");
            for (Object model:jsonArray) {
                JSONObject model1 = (JSONObject) model;
                Object pic_url = model1.get("pic_url");
                 new Thread(new Runnable() {
                    @Override
                    public void run() {
                        LocalDownJpg.download("http:"+pic_url,"淘宝/地勘机",null);
                    }
                }).start();
            }
            baos.close();
            is.close();
            //转换成json数据处理
//	        }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

}
